//
// Created by libtity on 24-5-24.
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 6789
#define IP "192.168.1.8"

int main() {
    char r_buf[100] = {0};
    char w_buf[100] = {0};
    struct sockaddr_in addr;
    int len = sizeof(addr);
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        perror("error socket");
        return -1;

    }
    printf("连接套接字创建成功\n");
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);


    connect(sockfd, (struct sockaddr *) &addr, len);

    while (1) {
        printf("客户端发送\n");
        memset(w_buf, 0, sizeof(w_buf));
        scanf("%s", w_buf);
        send(sockfd, w_buf, sizeof(w_buf), 0);

        memset(r_buf, 0, sizeof(r_buf));
        recv(sockfd, r_buf, sizeof(r_buf), 0);
        printf("客户端接收%s\n", r_buf);

    }
    close(sockfd);
    return 0;
}

